<?php


namespace App\Models;


use CodeIgniter\Model;

class BotModel extends Model
{
    protected $port_range_max;
    protected $port_range_min = 60000;
    protected $port_max = 65524;
    protected $table = 'bot';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $allowedFields = ['id', 'domain', 'host', 'port', 'account', 'device', 'create_time', 'secret_str', 'status'];


    public function __construct()
    {
        parent::__construct();
        $port_used_max = $this->getUsePortMax() + 5;
        if ($port_used_max >= $this->port_max) {
            $this->port_range_max = $this->port_max;
        } elseif ($port_used_max <= $this->port_range_min) {
            $this->port_range_max = $this->port_range_min + 5;
        } else $this->port_range_max = $port_used_max;
    }

    public function getUsePort($select = '*')
    {
        return $this->select($select)->findAll();
    }

    public function getUsePortMax()
    {
        $res = $this->selectMax('port')->find();
        if ($res) return $res[0]['port'];
        return null;

    }

    public function getRows($where_filed, $where_value, $where = [], $filed = '*')
    {
        $res = $this->select($filed)->where($where_filed, $where_value)->where($where)->find();
        if ($res) return $res[0];
        return null;
    }

}